#encoding = utf-8

#远程攻击的子弹模块
import pygame
import math
import player

class Bullet(pygame.sprite.Sprite):
    lstBullets = []

    '''
    构造函数
    参数：
    screen：屏幕
    player：玩家
    strImage：图片
    x：目的X轴
    y：目的Y轴
    '''
    def __init__(self, screen, winSize, player, strImagePath, strImage, x, y, ex, ey, baseAttack):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen    #屏幕
        self.winSize = winSize  #屏幕大小
        self.player = player    #玩家
        self.image = '' #实际使用图片
        self.imagePath = strImagePath   #文件路径
        self.imagePicture = strImage    #图片
        self.fullImage = ''    #子弹图片完整信息，含路径
        self.currentX = x #当前的x坐标
        self.currentY = y #当前的y坐标
        self.endX = ex  #结束的X坐标
        self.endY = ey  #结束的Y坐标
        self.interval = 10  #播放间隔
        self.rect = None
        self.width = 20 #宽度
        self.height = 20    #高度
        self.clear = False  #是否清除
        self.speed = 3  #速度
        self.currentFrame = 0   #当前帧，也就是子弹第几张图片
        self.baseAttack = baseAttack    #基本攻击值
        self.angle = 0  #轨迹的弧度
        self.bolUp = True   #默认向上
        self.bolLeft = True #默认向左

        #初始化数据
        self.fullImage = pygame.image.load(self.imagePath + self.imagePicture).convert_alpha()
        self.image = self.fullImage.subsurface((0, 0, self.width, self.height))
        self.rect = self.image.get_rect()
        #计算距离和sin和cos
        self.distance = math.sqrt(pow(self.currentX - self.endX, 2) + pow(self.currentY - self.endY, 2))
        self.cosa = (self.endX - self.currentX) / self.distance
        self.sina = (self.endY - self.currentY) / self.distance

        if self.endX > self.currentX:  # 向右
            self.bolLeft = False
        else:   #向左
            self.bolLeft = True
        if self.endY > self.currentY:  # 向下
            self.bolUp = False
        else:   #向上
            self.bolUp = True

        # print(self.bolUp, self.bolLeft)
        # print(self.sina, self.cosa)


    #类方法，用于添加子弹实例
    @classmethod
    def addBullets(cls, objBullets):
        cls.lstBullets.append(objBullets)

    #类方法，用于删除子弹实例，节省资源
    @classmethod
    def removeBullets(cls, objBullets):
        del cls.lstBullets[objBullets]

    def setAttribute(self, *attribute):
        interval = attribute.get['interval']
        width = attribute.get['width']
        height = attribute.get['height']
        speed = attribute.get['speed']
        if interval != "" or interval != None:
            self.interval = interval
        if width != "" or width != None:
            self.width = width
        if height != "" or height != None:
            self.height = height
        if speed != "" or speed != None:
            self.speed = speed

    '''
    以子弹为圆心计算到玩家的角度，0到360度
    参数：
    differX：玩家到怪物的X轴差值，为带符号的整数
    differY：玩家到怪物的Y轴差值，为带符号的整数
    '''
    def mathAngle(self, differX, differY):
        "计算到玩家的角度，传递的differX为以怪物为圆心，玩家到怪物的X轴的差值，需要带符号的整数，differY相同，返回值为0到360度的度数"
        angle = 360 - math.atan2(differY, differX) * (180 / math.pi)  # 角度
        if angle >= 360:
            angle -= 360
        return angle


    # def moveToPlayer(self):
    #     if not self.clear:
    #         if self.currentX == self.endX and self.currentY == self.endY:
    #             self.clear = True
    #         else:
    #             #x, y = pygame.mouse.get_pos()  # 获取鼠标位置，鼠标就是需要打击的目标
    #             distance = math.sqrt(pow(self.currentX - self.endX, 2) + pow(self.currentY - self.endY, 2))  # 两点距离公式
    #             section = self.speed * 1/60  # 每个时间片需要移动的距离
    #             sina = (self.currentY - self.endY) / distance
    #             cosa = (self.endX - self.currentX) / distance
    #             angle = math.atan2(self.endY - self.currentY, self.endX - self.currentX)  # 两点线段的弧度值
    #             self.currentX, self.currentY = (self.currentX + section * cosa, self.currentY - section * sina)
    #             #d_angle = math.degrees(angle)  # 弧度转角度
    #             #self.screen.blit(self.image, (self.currentX - self.image.get_width(), self.currentY - self.image.get_height() / 2))


    def moveToPlayer(self):
        if not self.clear:
            if self.currentY < -self.rect.height or self.currentY >= self.winSize[1]:
                self.clear = True
            if self.currentX < -self.rect.width or self.currentX >= self.winSize[0]:
                self.clear = True

            self.currentY += self.speed * self.sina
            self.currentX += self.speed * self.cosa


    '''
    播放子弹图片
    参数：
    direction：方向
    '''
    def playBulletImage(self):
        top = 0
        left = self.currentFrame * self.width
        rect = (left, top, self.width, self.height)
        fullImageRect = self.fullImage.get_rect()
        countLen = fullImageRect.width // self.width
        # 当为0的时候就更换
        if self.interval == 0:
            self.image = self.fullImage.subsurface(rect)
            self.currentFrame += 1
            if self.currentFrame >= countLen:
                self.currentFrame = 0


    def move(self):
        self.moveToPlayer()
        if self.interval == 0:
            self.playBulletImage()
            self.interval = 10
        else:
            self.interval -= 1


    def draw(self):
        self.move()

        #判断是否击中玩家
        if abs(self.player.rect.top - self.currentY) <= 20 and abs(self.player.rect.left - self.currentX) <= 20:
            self.player.reduceBlood(self.baseAttack)
            if self.player.blood <= 0:
                self.player.blood = 0
            self.clear = True

        self.screen.blit(self.image, (self.currentX, self.currentY))


#测试时代码
if __name__ == "__main__":
    pygame.init()
    winsize = (800, 600)
    screen = pygame.display.set_mode(winsize, 0, 32)
    pygame.display.set_caption("子弹类测试")
    p = player.Player(winsize, screen)
    b = bullet(screen, winsize, p, "images/bullet/", "littleFire.png", 0, 0, 700, 200, 2)
    framerate = pygame.time.Clock()
    while True:
        framerate.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        key = pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            exit()
        screen.fill((0, 0, 0))
        b.draw()
        #pygame.display.flip()
        pygame.display.flip()